import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rider/generated/assets.dart';
import 'package:flutter_rider/main/widgets/ext/u_button.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/module/mine/provider/mine_provider.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../generated/r.dart';
import '../ui_constant.dart';

// 带有取消和确定按钮的对话框(框架自带)
class CustomAlertDialog {
  static show(BuildContext context,
      {String title = "提示",
      String confirmText = "确定",
      String cancelTet = "取消",
      String content = "",
      Function? onConfirm,
      Function? onCancel}) {
    var dialog = AlertDialog(
      title: UText.get(title, UIColor.color_t1, 18.rpx, fw: FontWeight.w600),
      content: UText.get(content, UIColor.color_t3, 17.rpx),
      actions: [
        TextButton(
            onPressed: () {
              Navigator.pop(context);
              if (onCancel != null) {
                onCancel();
              }
            },
            child: UText.get(cancelTet, UIColor.color_t3, 15.rpx)),
        TextButton(
            onPressed: () {
              Navigator.pop(context);
              if (onConfirm != null) {
                onConfirm();
              }
            },
            child: UText.get(confirmText, UIColor.color_primary, 15.rpx)),
      ],
    );
    showDialog(
        context: context,
        builder: (context) {
          return dialog;
        });
  }

  // alert 弹框(框架自带)
  static alert(BuildContext context,
      {String title = "提示", String content = "", Function? onConfirm}) {
    var dialog = AlertDialog(
      title: UText.get(title, UIColor.color_t1, 18.rpx, fw: FontWeight.w600),
      content: UText.get(content, UIColor.color_t3, 17.rpx),
      actions: [
        TextButton(
            onPressed: () {
              Navigator.pop(context);
              onConfirm!();
            },
            child: UText.get("确定了", UIColor.color_primary, 15.rpx)),
      ],
    );
    showDialog(
        context: context,
        builder: (context) {
          return dialog;
        });
  }

  // 版本更新弹框
  static showUpdateVersionDialog(
      BuildContext context, Map obj, bool hasNewVersion, String title) {
    showDialog(
      context: context,
      builder: (context) {
        return Dialog(
            backgroundColor: Colors.transparent,
            child: Container(
              width: 320.rpx,
              //height: 244.rpx,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
              ),
              child: Column(
                children: [
                  32.hspace,
                  Container(
                    width: 64.rpx,
                    height: 64.rpx,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8.rpx)),
                    child: SvgPicture.asset(R.image_my_avatar,
                        width: 64.rpx, height: 64.rpx),
                  ),
                  8.hspace,
                  UText.get(title, UIColor.color_black, 16.rpx,
                      fw: FontWeight.bold),
                  8.hspace,
                  hasNewVersion
                      ? Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            RichText(
                              text: TextSpan(
                                text: "当前版本",
                                style: TextStyle(
                                  color: UIColor.color_t5,
                                  fontSize: 16.rpx,
                                ),
                                children: [
                                  TextSpan(
                                      text: "${obj["oldVersion"]}",
                                      style: const TextStyle(
                                          color: UIColor.color_primary)),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                text: "，发现新版本 ",
                                style: TextStyle(
                                  color: UIColor.color_t5,
                                  fontSize: 16.rpx,
                                ),
                                children: [
                                  TextSpan(
                                      text: "${obj["newVersion"]}",
                                      style: const TextStyle(
                                          color: UIColor.color_primary)),
                                ],
                              ),
                            ),
                          ],
                        )
                      : RichText(
                          text: TextSpan(
                            text: "当前已是最新版本 ",
                            style: TextStyle(
                              color: UIColor.color_t5,
                              fontSize: 16.rpx,
                            ),
                            children: [
                              TextSpan(
                                  text: "${obj["currVersion"]}",
                                  style: const TextStyle(
                                      color: UIColor.color_primary)),
                            ],
                          ),
                        ),
                  32.hspace,
                  Container(height: 0.5.rpx, color: UIColor.color_divider),
                  MaterialButton(
                      child: UText.get(hasNewVersion ? "检查更新" : "知道了",
                          UIColor.color_primary, 16.rpx,
                          fw: FontWeight.bold),
                      onPressed: () async {
                        // print("检查更新");
                        if (hasNewVersion) {
                          launch(obj["dl_url"]);
                          var _sp = await SharedPreferences.getInstance();
                          _sp.setInt("account", 1);
                        }
                        Navigator.of(context).pop();
                      }).size(56.rpx, double.infinity),
                ],
              ),
            ).intrinsicHeight);
      },
    );
  }

  // 主要用于工作台版本升级日志弹框
  static showVersionUpdateDialog(BuildContext context, Map obj) {
    showDialog(
      context: context,
      builder: (context) {
        return Dialog(
            backgroundColor: Colors.transparent,
            child: Container(
              width: 320.rpx,
              //height: 470.rpx, // 500
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
              ),
              child: Column(
                children: [
                  Container(
                    width: double.infinity,
                    height: 250.rpx,
                    //color: UIColor.color_primary,
                    decoration: const BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('assets/image/poi/version_up.png'),
                        fit: BoxFit.fill, // 完全填充
                      ),
                    ),
                  ),
                  Expanded(
                      child: Container(
                          decoration: const BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(8),
                                bottomRight: Radius.circular(8)),
                          ),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              24.hspace,
                              UText.get("版本升级", UIColor.color_black, 16.rpx,
                                  fw: FontWeight.bold),
                              16.hspace,
                              Expanded(
                                  child: SingleChildScrollView(
                                      child: Container(
                                width: double.infinity,
                                margin: EdgeInsets.only(
                                    left: 24.rpx, right: 24.rpx),
                                child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      UText.get(
                                          "智行助手${obj["newVersion"]}版本提测内容：",
                                          UIColor.color_t5,
                                          14.rpx),
                                      4.hspace,
                                      UText.get("${obj["content"]}",
                                          UIColor.color_t5, 14.rpx),
                                    ]),
                              ))),
                              24.hspace,
                              Container(
                                  height: 0.5.rpx,
                                  color: UIColor.color_divider),
                              MaterialButton(
                                  child: UText.get(
                                      "知道了", UIColor.color_primary, 16.rpx,
                                      fw: FontWeight.bold),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  }).size(56.rpx, double.infinity),
                            ],
                          )))
                ],
              ),
            ).intrinsicHeight);
      },
    );
  }

  // 选择工作中休息中
  static showWorkDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return Consumer(builder: (context, ref, child) {
          return Dialog(
              backgroundColor: Colors.transparent,
              child: Container(
                // width: 500.rpx,
                //height: 244.rpx,
                decoration: const BoxDecoration(
                  color: Colors.transparent,
                  // borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
                ),
                child: Column(
                  children: [
                    Container(
                      height: 72.rpx,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(12.rpx)),
                      ),
                      child: Row(
                        children: [
                          24.wspace,
                          SvgPicture.asset(
                            ref.watch(isWorkProvider)
                                ? Assets.imageMineBriefcaseH
                                : Assets.imageMineBriefcaseN,
                            width: 24.rpx,
                            height: 24.rpx,
                          ),
                          8.wspace,
                          UText.get(
                              "工作中",
                              ref.watch(isWorkProvider)
                                  ? UIColor.color_black
                                  : UIColor.color_t18,
                              16.rpx),
                          const Spacer(),
                          Visibility(
                              visible: ref.watch(isWorkProvider),
                              child: SvgPicture.asset(
                                Assets.imageOk,
                                width: 21.rpx,
                                height: 21.rpx,
                              )),
                          24.wspace,
                        ],
                      ),
                    ).onTap(() {
                      ref.read(isWorkProvider.notifier).setWork(context, true);
                    }),
                    12.hspace,
                    Container(
                      height: 72.rpx,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(12.rpx)),
                      ),
                      child: Row(
                        children: [
                          24.wspace,
                          SvgPicture.asset(
                            ref.watch(isWorkProvider)
                                ? Assets.imageMineCoffeeN
                                : Assets.imageMineCoffeeH,
                            width: 24.rpx,
                            height: 24.rpx,
                          ),
                          8.wspace,
                          UText.get(
                              "休息中",
                              ref.watch(isWorkProvider)
                                  ? UIColor.color_t18
                                  : UIColor.color_black,
                              16.rpx),
                          const Spacer(),
                          Visibility(
                              visible: !ref.watch(isWorkProvider),
                              child: SvgPicture.asset(
                                Assets.imageOk,
                                width: 21.rpx,
                                height: 21.rpx,
                              )),
                          24.wspace,
                        ],
                      ),
                    ).onTap(() {
                      ref.read(isWorkProvider.notifier).setWork(context, false);
                    }),
                    36.hspace,
                    SvgPicture.asset(
                      Assets.imageBigClose,
                      width: 50.rpx,
                      height: 50.rpx,
                    ).onTap(() {
                      NavigatorUtils.goBack(context);
                    }),
                  ],
                ),
              ).intrinsicHeight);
        });
      },
    );
  }

  // 带有取消和确定按钮的对话框(自定义)
  static showConfirmDialog(BuildContext context,
      {String title = "提示",
      String confirmText = "确定",
      String cancelTet = "取消",
      String content = "",
      Color confirmTextColor = UIColor.color_primary,
      Function? onConfirm,
      Function? onCancel,
      bool isShowCancel = true,
      bool barrierDismissible = true,
  }) {
    showDialog(
      barrierDismissible: barrierDismissible, //设置为false，点击空白处弹窗不关闭
      context: context,
      builder: (context) {
        List list = [];
        content.split("  ").forEach((element) {
          if (element != "") {
            list.add(element);
          }
        });
        return Dialog(
            backgroundColor: Colors.transparent,
            child: SizedBox(
                width: 320.rpx,
                child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 12.rpx),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(8.rpx)),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        32.hspace,
                        UText.get(title, UIColor.color_black, 16.rpx,
                            fw: FontWeight.bold),
                        8.hspace,
                        list.length == 1
                            ? UText.get(
                                list[0].toString(), UIColor.color_t5, 16.rpx)
                            : Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                    RichText(
                                      text: TextSpan(
                                        text: list[0].toString(),
                                        style: TextStyle(
                                          color: UIColor.color_t5,
                                          fontSize: 16.rpx,
                                        ),
                                        children: list.length<=3?[
                                        TextSpan(
                                            text:" ${list[1] ?? list[1].toString()}",
                                            style: const TextStyle(
                                                color: UIColor.color_primary,
                                                fontWeight: FontWeight.bold)
                                        ),
                                          TextSpan(
                                              text:" ${list[2] ?? list[2].toString()}",
                                              style: TextStyle(
                                                  color: UIColor.color_t5)
                                          ),
                                          ]:[
                                          TextSpan(
                                              text:" ${list[1] ?? list[1].toString()}",
                                              style: const TextStyle(
                                                  color: UIColor.color_primary,
                                                  fontWeight: FontWeight.bold)
                                          ),
                                          TextSpan(
                                              text:" ${list[2] ?? list[2].toString()}",
                                              style: TextStyle(
                                                  color: UIColor.color_t5)
                                          ),
                                          TextSpan(
                                              text:" ${list[3] ?? list[3].toString()}",
                                              style: TextStyle(
                                                  color: UIColor.color_primary,
                                                  fontWeight: FontWeight.bold)
                                          ),
                                          TextSpan(
                                              text:" ${list[4] ?? list[4].toString()}",
                                              style: TextStyle(
                                                  color: UIColor.color_t5)
                                          ),
                                        ],
                                      ),
                                    )
                                  ]
                        ),
                        32.hspace,
                        Container(
                            height: 0.5.rpx, color: UIColor.color_divider),
                        SizedBox(
                            height: 58.rpx,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Visibility(
                                    visible: isShowCancel,
                                    child: Expanded(
                                      child: TextButton(
                                          onPressed: () {
                                            Navigator.pop(context);
                                          },
                                          child: UText.get(cancelTet,
                                              UIColor.color_t15, 16.rpx,
                                              fw: FontWeight.w600)),
                                      flex: 1,
                                    )),
                                Visibility(
                                    visible: isShowCancel,
                                    child: Container(
                                        height: 58.rpx,
                                        width: 0.5.rpx,
                                        color: UIColor.color_divider)),
                                Expanded(
                                  child: TextButton(
                                      onPressed: () {
                                        Navigator.pop(context);
                                        if (onConfirm != null) {
                                          onConfirm();
                                        }
                                      },
                                      child: UText.get(
                                          confirmText, confirmTextColor, 16.rpx,
                                          fw: FontWeight.w600)),
                                  flex: 1,
                                ),
                              ],
                            ))
                      ],
                    ))).intrinsicHeight);
      },
    );
  }
}
